package org.apache.dolphinscheduler.api.service.impl;

import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.CorporateRemittanceService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.CorporateRemittance;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.CorporateRemittanceMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class CorporateRemittanceServiceImpl extends ServiceImpl<CorporateRemittanceMapper, CorporateRemittance> implements CorporateRemittanceService {

	public Result<Object> saveCR(User loginUser, CorporateRemittance corp) {
		Result<Object> result = new Result<>();
		if (loginUser.getRoleId().intValue() != 1) {
            result.setCode(Status.USER_NO_OPERATION_PERM.getCode());
        	result.setMsg(Status.USER_NO_OPERATION_PERM.getMsg());
            return result;
        }
		saveOrUpdate(corp);
		result.setData(corp);
		result.setCode(Status.SUCCESS.getCode());
    	result.setMsg(Status.SUCCESS.getMsg());
        return result;
	}
	
	public Result<Object> getCR(User loginUser) {
		Result<Object> result = new Result<>();
		QueryWrapper<CorporateRemittance> qw = new QueryWrapper<CorporateRemittance>();
		qw.orderByDesc("unit_name");
		qw.last("LIMIT 1");
		CorporateRemittance cr = getOne(qw);
		result.setData(cr);
		result.setCode(Status.SUCCESS.getCode());
    	result.setMsg(Status.SUCCESS.getMsg());
        return result;
	}
}
